<?php


namespace App\Http\Controllers\Front;


use App\Http\Controllers\Controller;
use App\Http\Service\MessageService;
use App\Model\Follow;
use Illuminate\Support\Facades\Auth;

class FollowController extends Controller
{
    /**
     * @SWG\Post(
     *     path="/front/follow/media/{id}",
     *     tags={"文章相关"},
     *     summary="关注自媒体",
     *     description="关注自媒体",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", required=true, type="integer", description="自媒体"),
     *     @SWG\Response(
     *          response="200",
     *          description="点赞成功"
     *      )
     * )
     */
    public function followMedia($id)
    {
        $old = Follow::where('media_user_id',$id)->where('user_id',Auth::id())->first();
        if ($old){
            if ($old->status == 1){
                return $this->fail('你已经关注过了');
            }else{
                $old->status=1;
                $old->save();
                return $this->success();
            }
        }

        Follow::create(['media_user_id'=>$id,'user_id'=>Auth::id(),'status'=> 1]);
        MessageService::message(Auth::id(), $id, 4, $id);
        return $this->success();
    }

    /**
     * @SWG\Post(
     *     path="/front/un_follow/media/{id}",
     *     tags={"文章相关"},
     *     summary="取消关注自媒体",
     *     description="取消关注自媒体",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", required=true, type="integer", description="自媒体"),
     *     @SWG\Response(
     *          response="200",
     *          description="点赞成功"
     *      )
     * )
     */
    public function unFollowMedia($id)
    {
        $old = Follow::where('media_user_id',$id)->where('user_id',Auth::id())->first();
        if ($old) {
            if ($old->status == 0){
                return $this->fail('你还没有关注ta');
            }else{
                $old->status = 0;
                $old->save();
                return $this->success();
            }
        }
        return $this->fail('你还没有关注ta');
    }
}